# Maintainer: Daniel Bermond <dbermond@archlinux.org>

pkgname=hm
pkgver=17.0
pkgrel=1
pkgdesc='HEVC Test Model - the reference software for HEVC'
arch=('loong64' 'x86_64')
url='https://hevc.hhi.fraunhofer.de/'
license=('BSD')
depends=('gcc-libs')
makedepends=('cmake' 'lsb-release' 'openmp')
source=("https://vcgit.hhi.fraunhofer.de/jct-vc/HM/-/archive/HM-${pkgver}/HM-HM-${pkgver}.tar.bz2"
        '010-hm-disable-werror.patch')
sha256sums=('3cff991240cc51af7855231d4ca6437ecf4453708c005c7eacf5a0d8e19aa506'
            '26ed4fb40812ecfce04b1301dc159fb2a575ece22a3532fe991f72e0f4e9488a')

prepare() {
    patch -d "HM-HM-${pkgver}" -Np1 -i "${srcdir}/010-hm-disable-werror.patch"
    cp -a "HM-HM-${pkgver}"{,-highbit}
}

build() {
    # fix kvazaar tests
    export CXXFLAGS+=' -Wp,-U_GLIBCXX_ASSERTIONS'
    
    local -a _common_opts=(
        '-DCMAKE_BUILD_TYPE:STRING=Release'
        '-DCMAKE_INSTALL_PREFIX:PATH=/usr'
        '-Wno-dev')
    
    cmake -B build -S "HM-HM-${pkgver}" "$_common_opts" -DHIGH_BITDEPTH:BOOL='OFF'
    make -C build
    
    cmake -B build-highbit -S "HM-HM-${pkgver}-highbit" "$_common_opts" -DHIGH_BITDEPTH:BOOL='ON'
    make -C build-highbit
}

package() {
    local _file
    while read -r -d '' _file
    do
        install -D -m755 "$_file" -t "${pkgdir}/usr/bin"
    done < <(find "HM-HM-${pkgver}/bin" -maxdepth 1 -type f -executable -print0)
    
    while read -r -d '' _file
    do
        install -D -m755 "HM-HM-${pkgver}-highbit/bin/${_file}" \
            "${pkgdir}/usr/bin/${_file/Static/HighBitDepthStatic}"
    done < <(find "HM-HM-${pkgver}-highbit/bin" -maxdepth 1 -type f -executable -print0 |
             sed -z "s|HM\-HM\-${pkgver}\-highbit/bin||")
    
    mv "${pkgdir}/usr/bin"/{parcatStatic,hm-parcatStatic}
    mv "${pkgdir}/usr/bin"/{parcatHighBitDepthStatic,hm-parcatHighBitDepthStatic}
    install -D -m644 "HM-HM-${pkgver}/doc"/*.pdf -t "${pkgdir}/usr/share/doc/${pkgname}"
    install -D -m644 "HM-HM-${pkgver}/COPYING" "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}
